package com.inspur.ydt.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import oracle.net.aso.p;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inspur.ydt.bean.PersonalUrls;
import com.inspur.ydt.service.PersonalService;

@Controller
@RequestMapping("/callApi")
public class CallBackController {

	 @RequestMapping(value="/callBack")
    @ResponseBody
   public void callBack(HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		 
		   	StringBuffer sb = new StringBuffer() ; 
		   	InputStream is = request.getInputStream(); 
		   	InputStreamReader isr = new InputStreamReader(is);   
		   	BufferedReader br = new BufferedReader(isr); 
		   	String s = "" ; 
		   	while((s=br.readLine())!=null){ 
		   		sb.append(s) ; 
		   	} 
	 
		    String param =sb.toString();
 
		    System.out.println("zzzzzzz"+param);
		   	JSONObject jsonObject = JSONObject.fromObject(param);
 		   	String urlParam = "order_no"+ "=" +jsonObject.get("order_no") +"&"
		   			        + "amount"+ "=" +jsonObject.get("amount") +"&"
		   			        + "nonce_str"+ "=" +jsonObject.get("nonce_str") +"&"
		   			        + "mchid"+ "=" +jsonObject.get("mchid") +"&"
		   			        + "pay_amt"+ "=" +jsonObject.get("pay_amt") +"&"
		   			        + "sign"+ "=" +jsonObject.get("sign") +"&"
		   			        + "trade_no"+ "=" +jsonObject.get("trade_no") +"&"
		   			        + "attach"+ "=" +jsonObject.get("attach") +"&"
		   			        + "pay_time"+ "=" +jsonObject.get("pay_time");
		    
 		   String gggString = sendGetRequest("http://prj-peixun.app-service-node.com/PayNotify?"+urlParam);
 		   System.out.println(gggString);
 		   	
	 }
 
	   public static String sendGetRequest(String getUrl)
	   {
		   System.out.println("getUrl"+getUrl);
	      StringBuffer sb = new StringBuffer();
	      InputStreamReader isr = null;
	      BufferedReader br = null;
	      try
	      {
	         URL url = new URL(getUrl);
	         URLConnection urlConnection = url.openConnection();
	         urlConnection.setAllowUserInteraction(false);
	         isr = new InputStreamReader(url.openStream());
	         br = new BufferedReader(isr);
	         String line;
	         while ((line = br.readLine()) != null)
	         {
	            sb.append(line);
	         }
	      }
	      catch (IOException e)
	      {
	         e.printStackTrace();
	      }
	     
	      return sb.toString();
	   }
	 
 	 
}
